package com.ten.system.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.ten.common.core.entity.AclUser;
import com.ten.security.annotation.RquiresPermissions;
import com.ten.security.utils.OssUtils;
import com.ten.system.service.AclUserService;
import com.ten.common.core.vo.Result;
import com.ten.system.vo.UserVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.net.URL;

/**
 * @author ：Q大侠
 * @date ：Created in 2022/12/1 21:41
 * @description：
 * @modified By：
 * @version:
 */
@RestController
@RequestMapping("/system/user")
@Api(tags = "用户接口类")
public class AclUserController {
    @Autowired
    private AclUserService aclUserService;

    @GetMapping("getByName/{username}")
    public Result<AclUser> getName(@PathVariable String username){
        return aclUserService.findByName(username);
    }

    /** 
     * @create by:  Q大侠
     * @description: 测试自定义的注解 
     * @create time: 2022/12/2 18:58
      * @param
     * @return  
     */
    @GetMapping("list")
    @RquiresPermissions(value = "user:list")
    public String test(){
        System.out.println("-------测试注解aop");
        return "aop注解测试!";
    }
    /** 
     * @create by:  Q大侠
     * @description: 获取当前登录的用户对象 
     * @create time: 2022/12/4 21:01
      * @param  
     * @return Result<AclUser> 
     */
    @GetMapping("info")
    public Result<AclUser> info() throws Exception {
        OssUtils ossUtils =new OssUtils();
        return aclUserService.findUserinfo();
    }
    /** 
     * @create by:  Q大侠
     * @description: 分页 
     * @create time: 2022/12/4 21:48
      * @param current,pageSize,userVo
     * @return Result<IPage<AclUser>> 
     */
    @PostMapping("getUserByPage/{current}/{pageSize}")
    public Result<IPage<AclUser>> getUserByPage(@PathVariable(required = true) Integer current,
                                                @PathVariable(required =true ) Integer pageSize,
                                                @RequestBody UserVo userVo){
        return aclUserService.findByConditionPage(current,pageSize,userVo);
    }
    @PostMapping("updateUserStatusByid/{id}/{deleted}")
    @ApiOperation(value = "根据用户id修改禁用/启用(is_deleted)")
    @ApiImplicitParams({@ApiImplicitParam(value = "用户id",name = "id"),@ApiImplicitParam(value = "禁用/启用",name = "deleted")})
    public Result<IPage<AclUser>> updateUserStatusByid(@PathVariable String id,@PathVariable Integer deleted){
        return aclUserService.updateUserStatusByid(id,deleted);
    }

    @PostMapping("addUserByUser")
    @ApiOperation(value = "添加管理员账户")
    public Result<IPage<AclUser>> addUserByUser (@RequestBody
    AclUser aclUser){
        return aclUserService.addUserByUser(aclUser);
    }

    @DeleteMapping("deleteUserById/{id}")
    @ApiOperation(value = "根据id删除管理员账户")
    @ApiImplicitParams({@ApiImplicitParam(value = "用户id",name = "id")})
    public Result<IPage<AclUser>> deleteUserById (@PathVariable String id){
        return aclUserService.deleteUserById(id);
    }

    @PostMapping("updateUserByUser")
    @ApiOperation(value = "根据用户名修改备注")
    @ApiImplicitParams({@ApiImplicitParam(value = "用户名",name = "id")})
    public Result<IPage<AclUser>> updateUserByUser (@RequestBody AclUser aclUser) {
        return aclUserService.updateUserByUser(aclUser);
    }

    @PostMapping("updateUserPassword/{oldPassword}/{newPassword}")
    @ApiOperation(value = "修改当前账号的密码")
    @ApiImplicitParams({@ApiImplicitParam(value = "原密码",name = "oldPassword"),
                        @ApiImplicitParam(value = "新密码",name = "newPassword")})
    public Result<IPage<AclUser>> updateUserPassword(@PathVariable String oldPassword,
                                                     @PathVariable String newPassword){
        return aclUserService.updateUserPassword(oldPassword,newPassword);
    }
    @PostMapping("uploadAvator")
    @ApiOperation(value = "修改当前账号的头像")
    @ApiImplicitParams({@ApiImplicitParam(value = "头像路径",name = "fileFormData")})
    public Result<AclUser> uploadAvator(@RequestParam("file") MultipartFile fileFormData){
        return aclUserService.uploadAvator(fileFormData);
    }
}
